<?php
/**
 * 题导入
 * User: cxm
 * Date: 2021/1/8
 * Time: 15:42
 */
namespace App\Http\Controllers\Admin\Question;

use App\Http\Controllers\Admin\Controller;
use App\Http\Controllers\Common\FileRequest;
use App\Jobs\QuestionUpload;
use App\Models\QuestionImportErrorLog;
use App\Models\QuestionImportRecord;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ImportController extends Controller{
    /**
     * 题导入
     * URL POST:/admin/question/import
     * @param FileRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function import(FileRequest $request){
        // 设置内存
        ini_set('memory_limit','-1');
        //设置超时限制为0分钟(无限制, 默认30s)
        set_time_limit(0);
        $file = $request->file('file');
        $originName = $file->getClientOriginalName();
        $fileextension = $file->getClientOriginalExtension();
        $lot = 'OD' . date("YmdHis", time()) . rand(1111, 9999);
        $fileName = $file->storeAs('excel', $lot . '_origin.'.$fileextension);
        $file_path = 'storage/'.$fileName;
        $data = Excel::import(new \App\Excel\QuestionImport($lot,$originName,$fileName),$file_path);
        // 删除上传的文件
        @unlink($file_path);
        dispatch(new QuestionUpload($lot))->onQueue('questionUpload');
        return $this->success('上传成功，后台正在进行数据处理，请耐心等待……');
    }

    /**
     * 查看处理的数据
     * URL POST:/admin/question/showData
     * @param Request $request
     * @param int status 1排队中 2处理中 3处理完成
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function showData(Request $request){
        $params = $request->only(['status','page','limit']);
        $status = isset($params['status']) ? $params['status'] : 0;
        if ($status > 0) {
            $params['where'][] = ['status',$status];
        }
        $params['orderBy'] = ['created_at desc'];
        $data = QuestionImportRecord::listByWhere($params);
        return $this->success('查询成功',$data);
    }

    /**
     * 删除结果
     * URL POST:/admin/question/importRecordDelete
     * @param Request $request
     * @param string lot 导入标识
     * @return mixed
     */
    public function importRecordDelete(Request $request){
        $lot = $request->post('ids','');
        if (empty($lot)) {
            return $this->error('缺少导入标识');
        }
        // 删除错误日志
        $error_log = QuestionImportErrorLog::whereIn('uuid',$lot)->forceDelete();
        if ($error_log === false) {
            return $this->error('错误日志删除失败');
        }
        // 删除记录
        $res = QuestionImportRecord::whereIn('uuid',$lot)->forceDelete();
        if ($res === false) {
            return $this->error('导入记录删除失败');
        }
        return $this->success('操作成功');
    }

    /**
     * 查看错误日志
     * URL POST:/admin/question/showErrorLog
     * @param Request $request
     * @param string lot 导入标识
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function showErrorLog(Request $request){
        $params = $request->only(['lot','page','limit']);
        if (!array_key_exists('lot',$params) || empty($params['lot'])) {
            return $this->error('缺少导入标识');
        }
        $params['orderBy'] = ['created_at desc'];
        $params['where'][] = ['lot',$params['lot']];
        $error_log = QuestionImportErrorLog::listByWhere($params);
        return $this->success('查询成功',$error_log);
    }

    /**
     * 查看错误日志--excel导出
     * URL POST:/admin/question/showErrorLogExport
     * @param Request $request
     * @param string lot 导入标识
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function showErrorLogExport(Request $request){
        $params = $request->only(['exportLot','page','limit']);
        if (!array_key_exists('exportLot',$params) || empty($params['exportLot'])) {
            return $this->error('缺少导入标识');
        }
        $params['orderBy'] = ['created_at desc'];
        $params['where'][] = ['lot',$params['exportLot']];
        $data = QuestionImportErrorLog::getAll($params);
        return Excel::download(new \App\Excel\ImportQuestionErrorExport($data), time().'_excel错误导出.xlsx');
    }

}